﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Admin_them_moi_nha_hang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetResType();
            LoadProvince();
        }
    }
    protected void GetResType()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandText = "SP_GetResType";
            Dal.CommandType = CommandType.StoredProcedure;
            DataTable db = Dal.GetDataTable();
            ddlResType.DataSource = db;
            ddlResType.DataTextField = db.Columns["MoTa"].ToString();
            ddlResType.DataValueField = db.Columns["LoaiNhaHangID"].ToString();
            ddlResType.DataBind();
            
        }
        catch
        {
            lblInfo.Text = "Lỗi khi load Loại Nhà Hàng";
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        try
        {
            if (fuAvatar.HasFile)
            {
                string filename = Path.GetFileName(fuAvatar.FileName);
                fuAvatar.SaveAs(Server.MapPath("~/Images/thumb-nhahang/"+filename));
            }
        }
        catch
        {
            lblInfo.Text = "Lỗi khi upload hình";
        }
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_AdRes";
            Dal.ParameterCollection = new string[18] { "@MoTa", "@LoaiNhaHangID", "@TenNhaHang", "@BaiGioiThieu", "@HinhDaiDien", "@DienThoai", "@Email", "@WebSite", "@FaceBook", "@TienIch", "@SucChua", "@IsActive", "@OnHome", "@TinhThanhPhoID", "@HuyenQuanID", "@XaPhuongID", "@Duong", "@SoNha" };
            Dal.ValueCollection = new string[18] {txtDescription.Text,ddlResType.SelectedValue,txtResName.Text,CKEditorControl1.Text,"Images/thumb-nhahang/"+fuAvatar.FileName,txtPhone.Text,txtEmail.Text,txtWebsite.Text,txtFB.Text,txtUtility.Text,txtCapacity.Text,RadioButtonList1.SelectedValue,rblPriority.SelectedValue,ddlProvince.SelectedValue,ddlDistrict.SelectedValue,ddlWard.SelectedValue,txtStreet.Text,txtHouseID.Text};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            lblInfo.Text = "Thêm mới thành công";
        }
        catch(Exception ex) {
            lblInfo.Text = ex.Message;
        }

    }
    protected void LoadProvince()
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllProvince";
            DataTable db = Dal.GetDataTable();
            ddlProvince.DataSource = db;
            ddlProvince.DataTextField = db.Columns["TenTinhThanhPho"].ToString();
            ddlProvince.DataValueField = db.Columns["TinhThanhPhoID"].ToString();
            ddlProvince.DataBind();
        }
        catch { }
    }
    protected void LoadDistrict()
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetDistrictByProvinceID";
            Dal.ParameterCollection = new string[1] { "@TinhThanhPhoID" };
            Dal.ValueCollection = new string[1] { ddlProvince.SelectedValue};
            DataTable db = Dal.GetDataTable();
            ddlDistrict.DataSource=db;
            ddlDistrict.DataTextField = db.Columns["TenHuyenQuan"].ToString();
            ddlDistrict.DataValueField = db.Columns["HuyenQuanID"].ToString();
            ddlDistrict.DataBind();
        }
        catch { }
    }
    protected void LoadWard()
    {
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetWardByDistrictID";
            Dal.ParameterCollection = new string[1] { "@HuyenQuanID" };
            Dal.ValueCollection = new string[1] { ddlDistrict.SelectedValue};
            DataTable db = Dal.GetDataTable();
            ddlWard.DataSource = db;
            ddlWard.DataTextField = db.Columns["TenXaPhuong"].ToString();
            ddlWard.DataValueField = db.Columns["XaPhuongID"].ToString();
            ddlWard.DataBind();
        }
        catch 
        { }
    }
    protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadDistrict();
    }
    protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadWard();
    }
}
